package liming.tool.pool;

import liming.tool.lock.CodeLock;
import liming.tool.pool.PoolPolicy.PutPolicy;
import liming.tool.pool.PoolPolicy.TakePolicy;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;


public class ObjectsPools {
    
    // 基础工厂方法
    public static <T> ObjectsPool<T> create(CodeLock codeLock,
                                           List<T> initList,
                                           int maxSize,
                                           PutPolicy putPolicy,
                                           TakePolicy takePolicy) {
        return new ObjectsPool<>(
            codeLock,
            new LinkedList<>(initList), // 默认使用LinkedList
            maxSize,
            putPolicy,
            takePolicy
        );
    }

    // 快捷方法：使用默认CodeLock
    public static <T> ObjectsPool<T> newDefaultPool(int maxSize) {
        return new ObjectsPool<>(
            new CodeLock(),
            new LinkedList<>(),
            maxSize,
            PutPolicy.BLOCK,
            TakePolicy.BLOCK
        );
    }

    // 快捷方法：使用LinkedList存储
    public static <T> ObjectsPool<T> newLinkedListPool(int maxSize,
                                                      PutPolicy putPolicy,
                                                      TakePolicy takePolicy) {
        return new ObjectsPool<>(
            new CodeLock(),
            new LinkedList<>(),
            maxSize,
            putPolicy,
            takePolicy
        );
    }

    // 快捷方法：使用ArrayList存储
    public static <T> ObjectsPool<T> newArrayListPool(int maxSize,
                                                     PutPolicy putPolicy,
                                                     TakePolicy takePolicy) {
        return new ObjectsPool<>(
            new CodeLock(),
            new ArrayList<>(),
            maxSize,
            putPolicy,
            takePolicy
        );
    }

    // 快捷方法：带初始数据
    public static <T> ObjectsPool<T> newInitializedPool(List<T> initData,
                                                       int maxSize,
                                                       PutPolicy putPolicy,
                                                       TakePolicy takePolicy) {
        return new ObjectsPool<>(
            new CodeLock(),
            new LinkedList<>(initData),
            maxSize,
            putPolicy,
            takePolicy
        );
    }

    // 快捷方法：无界池（maxSize = Integer.MAX_VALUE）
    public static <T> ObjectsPool<T> newUnboundedPool(TakePolicy takePolicy) {
        return new ObjectsPool<>(
            new CodeLock(),
            new LinkedList<>(),
            0,
            PutPolicy.REMOVE_OLDEST,
            takePolicy
        );
    }
}